package com.hangim.pce.enums;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 频次单位
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum TimeUnit {
    SECOND("SECOND", 1, "s/秒"),
    MINUTE("MINUTE", 2, "m/分钟"),
    HOUR("HOUR", 3, "h/小时"),
    DAY("DAY", 4, "d/日/天"),
    MONTH("MONTH", 5, "m/月"),
    YEAR("YEAR", 6, "y/年"),
    OTHER("OTHER", 0, "其他");
    // 类型
    private final String code;
    // 描述
    private final String desc;
    // 编号
    private final Integer num;

    TimeUnit(final String code, final Integer num, final String desc) {
        this.code = code;
        this.desc = desc;
        this.num = num;
    }

    /**
     * 把所有枚举定义转换成实体
     *
     * @return List<TimeUnit>
     */
    public static List<TimeUnit> getList() {
        List<TimeUnit> list = Lists.newArrayList();

        TimeUnit[] ts = TimeUnit.values();
        for (TimeUnit type : ts) {
            list.add(type);
        }
        return list;
    }

    public static TimeUnit getFreqUnit(String code) {
        TimeUnit[] ts = TimeUnit.values();
        for (TimeUnit t : ts) {
            if (t.getCode().equalsIgnoreCase(code)) {
                return t;
            }
        }
        return OTHER;
    }

    public static TimeUnit getTimeUnitByDesc(String desc) {
        TimeUnit[] ts = TimeUnit.values();
        for (TimeUnit t : ts) {
            if (t.getDesc().contains(desc)) {
                return t;
            }
        }
        return OTHER;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public Integer getNum() {
        return num;
    }
}
